package com.hqq.helper;

import com.hqq.util.ReflectionUtil;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;

/**
 * BeanHelper
 * 获取相关的bean实例
 * Created by heqianqian on 2017/7/19.
 */
public class BeanHelper {

    private static Map<Class<?>, Object> BEAN_MAP;

    static {
        BEAN_MAP = new HashMap<>();
        Set<Class<?>> beans = ClassHelper.getBeans();
        for (Class clazz : beans) {
            Object instance = ReflectionUtil.getInstance(clazz);
            BEAN_MAP.put(clazz, instance);
        }
    }

    public static Map<Class<?>, Object> getBeanMap() {
        return BEAN_MAP;
    }

    public static <T> void setBean(Class<T> clazz, Object object) {
        BEAN_MAP.put(clazz, object);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        if (BEAN_MAP.containsKey(clazz)) {
            return (T) BEAN_MAP.get(clazz);
        }
        throw new RuntimeException("Bean is Not Exists!");
    }

}
